
import { getStoreDetail, getCoupon, pullDownCoupon, getStoreList } from '@src/api/mall/store'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from '@src/store/base'
import mall from '@src/store/mall'
import router from "@src/router";


Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ["homePage"]
    },
    {
      store: mall,
      fields: ['curLocation', 'storeId'],
      actions: ['setStoreId']
    }
  ],
  properties: {
    storeInfo: {
      type: Object,
      value: {}
    },
    login: {
      type: Boolean
    },
  },
  observers: {
    curLocation(curLocation) {
      if(!curLocation) {
        if(!this.data.login) {
          return
        }
        getStoreList({})
        .then(res => {
          this.setData({storeList: res.data})
        })
        return
      }
      const { longitude, latitude } =  curLocation
      
      getStoreList({latitude, longitude})
        .then(res => {
          this.setData({storeList: res.data})
        })
    },
    homePage(homePage) {
      if(!homePage.modules) return
      const modules = this.data.homePage.modules
      const shangpinfenlei = modules.filter(item => item.type === 'shangpinfenlei')[0]
      const menuIdList = JSON.parse(shangpinfenlei.biz.params)?.menuIdList
      if(menuIdList) {
        this.setData({menuIdList})
      }
    }
  },
  data: {
    fileBaseUrl: null,
    menuIdList: [],
    coupon: [],
    storesShow: false,
    storeList: [],
    isShowCouponDown: true,
    filterList: [
      {
      id: "sort",
      title: "距离远近",
      type: "single",
      data: [
        { id: 1, title: "距离从近到远" },
        { id: 2, title: "距离从远到近" }
      ]
    }, {
      id: "buyWay",
      title: "购买方式",
      type: "single",
      data: [
        { id: 0, title: "外卖配送", str: 'LOCAL' },
        { id: 1, title: "到店自提", str: 'SELF_PICKUP' },
        { id: 2, title: "快递邮寄", str: 'EXPRESS' }
      ]
    }
  ]
    
  },
  lifetimes: {
    ready() {
      this.setData({
        fileBaseUrl: getApp().globalData.extConfig.fileBaseUrl
      })
      
      this.getCoupon()
      getApp().globalData.storeAboutQuery = this.calcHeight()
    },
  },
  methods: {
    calcHeight() {
      return new Promise((resolve) => {
        let query = this.createSelectorQuery()
        query.select('.dianpujianjie').boundingClientRect()
        resolve(query);

      })
    },
    toCouponCenter() {
      router.push({
        name: 'member/couponCenter'
      })
    },
    getStoreList(e) {
      if(!this.data.login) {
        return
      }
      const { detail } = e
      if(detail[0] == 0 && detail[1].id == 1) {
        let storeList = this.data.storeList
        storeList.sort((a, b) => a.distance - b.distance)
        this.setData({storeList})
      }else if(detail[0] == 0 && detail[1].id == 2) {
        let storeList = this.data.storeList
        storeList.sort((a, b) => b.distance - a.distance )
        this.setData({storeList})
      }
      
      if(detail[0] == 1 &&  detail[1].str) {
        getStoreList({type: detail[1].str, 
          latitude: this.data.curLocation.latitude, 
          longitude:this.data.curLocation.longitude 
        })
        .then(res => {
          this.setData({storeList: res.data})
        })
      }
    },
    enterStore({target}) {
      const { store } = target.dataset
      if(!store.disabled) {
        this.setStoreId(store.storeId)
        this.updateStoreBindings();
        this.setData({ 
          storesShow: false,
         })
      }
    },
    openSwitchStore() {
      if(!this.data.login) {
        this.triggerEvent('login');
        return
      }
      this.setData({storesShow: true})
    },
    pullDownCoupon({target}) {
      const {receivecode, index} = target.dataset
      let { coupon } = this.data
      if(coupon[index].pullDown) return
      pullDownCoupon({receiveCode:receivecode }) 
      .then(res => {
        if(res.code == 0) {
          coupon[index].pullDown = true
          this.setData({coupon})
          wx.showToast({
            title: '领取成功'
          })
        }
      })
    },
    getCoupon() {
      if(!this.data.login) return
      getCoupon({})
      .then(res => {
        let { data } = res
        let filter = JSON.parse(JSON.stringify(data.filter(item => item.couponType !== 'exchange' && item.status == 2))).slice(0, 2)
        filter.length === 0 && (this.setData({isShowCouponDown: false}))
        filter.forEach(item => {item.pullDown = false})
        this.setData({coupon: filter})
        
      }).catch(err => {
        console.log(err)
        
      })
    },
    getStoreInfo() {
      // if(!this.data.login) {
      //   return
      // }
      getStoreDetail({storeId: this.data.storeId})
      .then(res => {
        this.setData({storeInfo: res.data})
        console.log(res.data)
      })
    },
    toSearchPage() {
      if(!this.data.login) {
        this.triggerEvent('login');
        return
      }
      wx.navigateTo({
        url: '/sp-mall/pages/store-search/index?menuIdList=' + this.data.menuIdList
      })
    }
  }
})
